package com.group56.view.coachend.schedule_module;

import com.group56.CoachApp;
import com.group56.factory.CoachAppCMFactory;
import com.group56.utils.FXMLSrcMenu;
import com.group56.view.ViewController;
import com.group56.viewmodel.CoachScheduleVM;
import com.group56.viewmodel.dto.CoachScheduleListDTO;
import com.group56.viewmodel.dto.ScheduleDTO;
import javafx.beans.property.StringProperty;
import javafx.collections.ObservableList;
import javafx.collections.ObservableMap;
import javafx.scene.Parent;
import javafx.scene.control.Alert;
import javafx.scene.control.Label;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.VBox;

import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class CoachScheduleController implements ViewController {
    public VBox day1vBox;
    public VBox day2vBox;
    public VBox day3vBox;
    public VBox day4vBox;
    public VBox day5vBox;
    public Label date1;
    public Label date2;
    public Label date3;
    public Label date4;
    public Label date5;

    public CoachScheduleVM getCoachScheduleVM() {
        return coachScheduleVM;
    }

    private CoachScheduleVM coachScheduleVM = CoachScheduleVM.getInstance(CoachApp.coachId);

    public CoachScheduleController() throws FileNotFoundException {
    }

    public void initialize(){
        inject();
//        for(int i=0; i<8; i++){
//            day1vBox.getChildren().add(new ScheduleRectController(ScheduleRectController.STATUS.BOOKED).getPane());
//            day2vBox.getChildren().add(new ScheduleRectController(ScheduleRectController.STATUS.UNBOOKED).getPane());
//            day3vBox.getChildren().add(new ScheduleRectController(ScheduleRectController.STATUS.UNBOOKED).getPane());
//            day4vBox.getChildren().add(new ScheduleRectController(ScheduleRectController.STATUS.AVAILABLE).getPane());
//            day5vBox.getChildren().add(new ScheduleRectController(ScheduleRectController.STATUS.AVAILABLE).getPane());
//        }
    }
    @Override
    public void inject() {
        ObservableMap<StringProperty, ObservableList<ScheduleDTO>> map = this.getCoachScheduleVM().getScheduleListDTO().getScheduleDTOMap();
//        ScheduleRectController srController = new ScheduleRectController(this, ScheduleRectController.STATUS.BOOKED);
//        srController.inject();
        List<StringProperty> dates = new ArrayList<>();
        map.forEach((date, scheduleList)->{
            dates.add(date);
        });
        date1.textProperty().bindBidirectional(dates.get(0));
        date2.textProperty().bindBidirectional(dates.get(1));
        date3.textProperty().bindBidirectional(dates.get(2));
        date4.textProperty().bindBidirectional(dates.get(3));
        date5.textProperty().bindBidirectional(dates.get(4));
        for(ScheduleDTO scheduleDTO : map.get(dates.get(0))){
            if(scheduleDTO.getStatus() == ScheduleRectController.STATUS.BOOKED) {
                ScheduleRectController scheduleRectController = new ScheduleRectController(scheduleDTO, ScheduleRectController.STATUS.BOOKED);
                day1vBox.getChildren().add(scheduleRectController.getPane());
            }
            if(scheduleDTO.getStatus() == ScheduleRectController.STATUS.UNBOOKED) {
                ScheduleRectController scheduleRectController = new ScheduleRectController(scheduleDTO, ScheduleRectController.STATUS.UNBOOKED);
                day1vBox.getChildren().add(scheduleRectController.getPane());
            }
            if(scheduleDTO.getStatus() == ScheduleRectController.STATUS.UNAVAILABLE) {
                ScheduleRectController scheduleRectController = new ScheduleRectController(scheduleDTO, ScheduleRectController.STATUS.UNAVAILABLE);
                scheduleRectController.setReadOnly();
                day1vBox.getChildren().add(scheduleRectController.getPane());
            }
        }
        for(ScheduleDTO scheduleDTO : map.get(dates.get(1))){
            if(scheduleDTO.getStatus() == ScheduleRectController.STATUS.BOOKED) {
                ScheduleRectController scheduleRectController = new ScheduleRectController(scheduleDTO, ScheduleRectController.STATUS.BOOKED);
                day2vBox.getChildren().add(scheduleRectController.getPane());
            }
            if(scheduleDTO.getStatus() == ScheduleRectController.STATUS.UNBOOKED) {
                ScheduleRectController scheduleRectController = new ScheduleRectController(scheduleDTO, ScheduleRectController.STATUS.UNBOOKED);
                day2vBox.getChildren().add(scheduleRectController.getPane());
            }
            if(scheduleDTO.getStatus() == ScheduleRectController.STATUS.UNAVAILABLE) {
                ScheduleRectController scheduleRectController = new ScheduleRectController(scheduleDTO, ScheduleRectController.STATUS.UNAVAILABLE);
                scheduleRectController.setReadOnly();
                day2vBox.getChildren().add(scheduleRectController.getPane());
            }
        }
        for(ScheduleDTO scheduleDTO : map.get(dates.get(2))){
            if(scheduleDTO.getStatus() == ScheduleRectController.STATUS.BOOKED) {
                ScheduleRectController scheduleRectController = new ScheduleRectController(scheduleDTO, ScheduleRectController.STATUS.BOOKED);
                day3vBox.getChildren().add(scheduleRectController.getPane());
            }
            if(scheduleDTO.getStatus() == ScheduleRectController.STATUS.UNBOOKED) {
                ScheduleRectController scheduleRectController = new ScheduleRectController(scheduleDTO, ScheduleRectController.STATUS.UNBOOKED);
                day3vBox.getChildren().add(scheduleRectController.getPane());
            }
            if(scheduleDTO.getStatus() == ScheduleRectController.STATUS.UNAVAILABLE) {
                ScheduleRectController scheduleRectController = new ScheduleRectController(scheduleDTO, ScheduleRectController.STATUS.UNAVAILABLE);
                scheduleRectController.setReadOnly();
                day3vBox.getChildren().add(scheduleRectController.getPane());
            }
        }
        for(ScheduleDTO scheduleDTO : map.get(dates.get(3))){
            if(scheduleDTO.getStatus() == ScheduleRectController.STATUS.UNAVAILABLE) {
                ScheduleRectController scheduleRectController = new ScheduleRectController(scheduleDTO, ScheduleRectController.STATUS.UNAVAILABLE);
                day4vBox.getChildren().add(scheduleRectController.getPane());
            }
            if(scheduleDTO.getStatus() == ScheduleRectController.STATUS.AVAILABLE) {
                ScheduleRectController scheduleRectController = new ScheduleRectController(scheduleDTO, ScheduleRectController.STATUS.AVAILABLE);
                day4vBox.getChildren().add(scheduleRectController.getPane());
            }
        }
        for(ScheduleDTO scheduleDTO : map.get(dates.get(4))){
            if(scheduleDTO.getStatus() == ScheduleRectController.STATUS.UNAVAILABLE) {
                ScheduleRectController scheduleRectController = new ScheduleRectController(scheduleDTO, ScheduleRectController.STATUS.UNAVAILABLE);
                day5vBox.getChildren().add(scheduleRectController.getPane());
            }
            if(scheduleDTO.getStatus() == ScheduleRectController.STATUS.AVAILABLE) {
                ScheduleRectController scheduleRectController = new ScheduleRectController(scheduleDTO, ScheduleRectController.STATUS.AVAILABLE);
                day5vBox.getChildren().add(scheduleRectController.getPane());
            }
        }


    }

    @Override
    public void bind() {
        ViewController.super.bind();
    }

    @Override
    public void registerToMediator() {
        CoachAppCMFactory.getCtrlrMediator().registerController(FXMLSrcMenu.CoachEnd.APP_FRAMEWORK.getSrc(),this);
    }

    @Override
    public Parent getView() {
        return null;
    }

    public void clickConfirm(MouseEvent mouseEvent) {
        coachScheduleVM.updateToModel();
        Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
        alert.setHeaderText("modify confirmed!");
        alert.show();
        coachScheduleVM.updateFromModel();
    }
}
